package com.llc.gulimall.order.config;

import com.llc.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author llc
 * @Description
 * @Version
 * @date 2022/2/14 23:29
 */
@Slf4j
@Configuration
public class MyRabbitConfig {
    @Autowired
    AmqpAdmin amqpAdmin;
    //@Bean Binding Queue Exchange
    //
    //@RabbitListener(queues = "order.release.order.queue")
    //public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
    //    System.out.println("收到过期的订单消息"+entity);
    //    channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    //}


    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);
        Queue queue = new Queue("order.delay.queue",true,false,false,arguments);
        return queue;
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        //Map<String,Object> arguments = new HashMap<>();
        Queue queue = new Queue("order.release.order.queue",true,false,false);
        return queue;
    }

    /**
     *
     * @author llc
     * @description  消费队列 秒杀订单
     * @date 2022/2/21 1:21
     * @param
     * @return org.springframework.amqp.core.Queue
     */

    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue("order.seckill.order.queue",true,false,false);
    }


    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,false);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.create.order",null);
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release.order.#",null);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        return new Binding("order.seckill.order.queue",Binding.DestinationType.QUEUE,
                "order-event-exchange","order.seckill.order",null);
    }
    /**
     *
     * @author llc
     * @description  订单释放和库存直接绑定
     * @date 2022/2/18 1:30
     * @param
     * @return org.springframework.amqp.core.Binding
     */

    @Bean
    public Binding orderReleaseOtherBinding(){

        return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release.other.#",null);
    }



    //@Bean
    //public MessageConverter messageConverter(){
    //    return new Jackson2JsonMessageConverter();
    //}
    @Autowired
    RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void initRabbitTemplate(){
        //设置确认回调,消息抵达服务器就是true
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("Confirm ...correlationData"+correlationData+"]==>acl["+ack+"]");
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            /**
             *
             * @author llc
             * @description 确认消息投递
             * @date 2022/2/18 13:34
             * @param message
             * @param replyCode
             * @param replyText
             * @param exchange
             * @param routingKey
             * @return void
             */

            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("Fail message"+message);
            }
        });
    }
}


